package com.koron.css2.newProjectCenter.print;

import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.WaterTypeBean;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.mapper.WaterTypeMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoQueryBean;
import com.koron.css2.meterService.dto.MsInfoDto;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjPrintQuery;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.PrintInterfaceImpl;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class NonResidentWaterSupplyContract implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjPrintQuery pjPrintQuery = JsonUtils.objectToPojo(req.getData(), PjPrintQuery.class);
            // 1. 获取打印数据
            ArrayList<HashMap<String, String>> result = getPrintDate(factory, pjPrintQuery);
            // 2. 生成打印id
            String key = PrintInterfaceImpl.setPrintData(result);
            List<String> keys = new ArrayList<String>();
            keys.add(key);
            // 3. 返回打印id
            MessageBean<List> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "非居民供水合同获取打印数据成功", List.class);
            messageBean.setData(keys);
            return messageBean;
        } catch (Exception e) {
            logger.error("非居民供水合同获取打印数据失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "非居民供水合同获取打印数据失败", null);
        }
    }

    public static ArrayList<HashMap<String, String>> getPrintDate(SessionFactory factory, PjPrintQuery pjPrintQuery) {
        ArrayList<HashMap<String, String>> result = new ArrayList<HashMap<String, String>>();

        CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
        PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
        UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);
        MeterBoreMapper boreMapper = factory.getMapper(MeterBoreMapper.class);
        MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);

        // 1. 获取需要打印的水表数据
        List<PjMeter> pjMeterList = pjMeterMapper.getListByIdList(pjPrintQuery.getPjMeterIdList());
        // 2. 客户信息
        List<String> userCtmTempIdList = pjMeterList.stream().map(PjMeter::getUserCtmTempId).collect(Collectors.toList());
        List<CtmInfoBean> ctmInfoBeanList = ctmInfoMapper.getListByIdList(userCtmTempIdList, CtmInfoBean.CTMTEMP);
        // 3. 用户信息
        List<String> userInfoTempIdList = pjMeterList.stream().map(PjMeter::getUserInfoTempId).collect(Collectors.toList());
        UserInfoQueryBean userInfoQueryBean = new UserInfoQueryBean();
        userInfoQueryBean.setIdList(userInfoTempIdList);
        List<UserBean> userBeanList = userInfoMapper.getList(userInfoQueryBean, UserBean.USERTEMP);
        // 4. 水表口径
        Map<String, String> boreMap = null;
        List<MeterBoreVO> meterBoreVOList = boreMapper.selectMeterBoreList(new MeterBoreQueryBean());
        boreMap = meterBoreVOList.stream().collect(Collectors.toMap(MeterBoreVO::getId, MeterBoreVO::getBoreName));
        for (int i = 0; i < userBeanList.size(); i++) {
            HashMap<String, String> map = new HashMap<>();
            CtmInfoBean ctmInfoBean;
            if (ctmInfoBeanList.size() == 1) {
                ctmInfoBean = ctmInfoBeanList.get(0);
            } else {
                ctmInfoBean = ctmInfoBeanList.get(i);
            }
            // 5.1 ctmAddr
            map.put("ctmAddr", ctmInfoBean.getCtmAddr());
            // 5.2 ctmName
            map.put("ctmName", ctmInfoBean.getCtmName());
            // 5.3 mobile
            map.put("mobile", ctmInfoBean.getMobile());
            // 5.4 certNo
            map.put("certNo", ctmInfoBean.getCertNo());
            // 5.5 setupMeterAddr
            UserBean userBean = userBeanList.get(i);
            map.put("setupMeterAddr", userBean.getSetupMeterAddr());
            // 5.6 userNo
            map.put("userNo", userBean.getUserNo());
            // 5.7 contractNo
            map.put("contractNo", userBean.getContractNo());
            // 5.8 contractDate
            map.put("contractDate", userBean.getContractDate());
            // 5.9 useWaterTypeName
            String useWaterType = userBean.getUseWaterType();
            if (StringUtils.isNotEmpty(useWaterType)) {
                WaterTypeMapper waterTypeMapper = factory.getMapper(WaterTypeMapper.class);
                WaterTypeBean waterTypeBean = waterTypeMapper.findWaterTypeById(useWaterType);
                map.put("useWaterTypeName", waterTypeBean.getWaterTypeName());
            }
            // 5.10 meterBoreName
            PjMeter pjMeter = pjMeterList.get(i);
            MsInfoDto msInfoDto = msInfoMapper.getMsInfoById(pjMeter.getMsInfoId());
            if (msInfoDto != null) {
                map.put("meterBoreName", boreMap.get(msInfoDto.getMeterBore()));
            }
            result.add(map);
        }
        return result;
    }
}
